<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/greenwood.gitee.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/greenwood.gitee.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/greenwood.gitee.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/greenwood.gitee.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/greenwood.gitee.io/css/main.css">


<link rel="stylesheet" href="/greenwood.gitee.io/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/greenwood.gitee.io/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="CS144 LAB0Fetch a Web pagestep1使用浏览器访问http:&#x2F;&#x2F;cs144.keithw.org&#x2F;hello，发现只有一行文字：Hello, CS144!">
<meta property="og:type" content="article">
<meta property="og:title" content="CS144 lab0">
<meta property="og:url" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/index.html">
<meta property="og:site_name" content="我看刑">
<meta property="og:description" content="CS144 LAB0Fetch a Web pagestep1使用浏览器访问http:&#x2F;&#x2F;cs144.keithw.org&#x2F;hello，发现只有一行文字：Hello, CS144!">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/image-20211017231855278.png">
<meta property="og:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/image-20211017232127110.png">
<meta property="og:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/image-20211017232747553.png">
<meta property="og:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/image-20211018085619537.png">
<meta property="og:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/image-20211018094018241.png">
<meta property="article:published_time" content="2021-10-17T16:57:03.000Z">
<meta property="article:modified_time" content="2021-10-17T17:46:44.751Z">
<meta property="article:author" content="Dybala">
<meta property="article:tag" content="计算机网络">
<meta property="article:tag" content="CS144">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/image-20211017231855278.png">

<link rel="canonical" href="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>CS144 lab0 | 我看刑</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/greenwood.gitee.io/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">我看刑</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/greenwood.gitee.io/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/greenwood.gitee.io/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/greenwood.gitee.io/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/greenwood.gitee.io/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/greenwood.gitee.io/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/18/lab0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/greenwood.gitee.io/images/avatar.gif">
      <meta itemprop="name" content="Dybala">
      <meta itemprop="description" content="日子越来越有判头了">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我看刑">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          CS144 lab0
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-10-18 00:57:03 / 修改时间：01:46:44" itemprop="dateCreated datePublished" datetime="2021-10-18T00:57:03+08:00">2021-10-18</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/greenwood.gitee.io/categories/CS144%E5%AE%9E%E9%AA%8C/" itemprop="url" rel="index"><span itemprop="name">CS144实验</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="CS144-LAB0"><a href="#CS144-LAB0" class="headerlink" title="CS144 LAB0"></a>CS144 LAB0</h1><h2 id="Fetch-a-Web-page"><a href="#Fetch-a-Web-page" class="headerlink" title="Fetch a Web page"></a>Fetch a Web page</h2><h3 id="step1"><a href="#step1" class="headerlink" title="step1"></a>step1</h3><p>使用浏览器访问<a target="_blank" rel="noopener" href="http://cs144.keithw.org/hello，发现只有一行文字：Hello">http://cs144.keithw.org/hello，发现只有一行文字：Hello</a>, CS144!</p>
<span id="more"></span>
<h3 id="step2"><a href="#step2" class="headerlink" title="step2"></a>step2</h3><p>命令行中输入telnet cs144.keithw.org http<br>随后依次输入：</p>
<ul>
<li><p>GET /hello HTTP/1.1并敲入回车。这一行指令表示向服务器发送在HTTP/1.1下获取路径为/hello的数据的请求。</p>
</li>
<li><p>Host: cs144.keithw.org并敲入回车。这一行指令告诉服务器host地址。</p>
</li>
<li><p>Connection: close并敲入回车。这一行指令告诉服务器客户的请求已经发送完毕，服务器只要发送完数据就可以断开连接。<br>注意此时还需要敲一下回车。<br>如果一切都顺利的话，应该能看到下图中的结果：</p>
<p><img src="image-20211017231855278.png" alt="image-20211017231855278"></p>
</li>
</ul>
<h3 id="step3-拓展实验"><a href="#step3-拓展实验" class="headerlink" title="step3 拓展实验"></a>step3 拓展实验</h3><p>Use the above technique to fetch the URL <a target="_blank" rel="noopener" href="http://cs144.keithw.org/lab0/sunetid">http://cs144.keithw.org/lab0/sunetid</a>, replacing sunetid with your own primary SUNet ID. You will receive a secret code in the X-Your-Code-Is: header.<br>就是访问以上那个URL,将sunetid替换成我想要的,然后重复上面的过程,得到X-Your-Code,这个东西在header里面.<br>如图所示:</p>
<p><img src="image-20211017232127110.png" alt="image-20211017232127110"></p>
<h2 id="Listening-and-connecting"><a href="#Listening-and-connecting" class="headerlink" title="Listening and connecting"></a>Listening and connecting</h2><h3 id="step1-1"><a href="#step1-1" class="headerlink" title="step1"></a>step1</h3><p>在一个终端中键入nc -vlnp 9090创建服务器开启监听</p>
<h3 id="step2-1"><a href="#step2-1" class="headerlink" title="step2"></a>step2</h3><p>在另一个终端中键入telnet localhost 9090连接服务器</p>
<h3 id="step3"><a href="#step3" class="headerlink" title="step3"></a>step3</h3><p>在任意一个终端中发送信息,另一个终端都会同步更新,如下图<br><img src="image-20211017232747553.png" alt="image-20211017232747553"></p>
<h2 id="Writing-a-network-program-using-an-OS-stream-socket"><a href="#Writing-a-network-program-using-an-OS-stream-socket" class="headerlink" title="Writing a network program using an OS stream socket"></a>Writing a network program using an OS stream socket</h2><p>相应的介绍如下:<br><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">In the next part of this warmup lab, you will write a short program that fetches a Web page over the Internet. </span><br><span class="line">You’ll write a program called “webget” that creates a TCP stream socket, connects to a Web server, and fetches a page—much as you did earlier in this lab.</span><br></pre></td></tr></table></figure></p>
<h3 id="1-fetching-and-building-the-starter-code"><a href="#1-fetching-and-building-the-starter-code" class="headerlink" title="1. fetching and building the starter code"></a>1. fetching and building the starter code</h3><ul>
<li>项目需要起始代码,执行下面命令获取<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> https://github.com/cs144/sponge </span><br></pre></td></tr></table></figure></li>
<li>cd sponge</li>
<li>mkdir build</li>
<li>cd build</li>
<li>cmake ..<strong>缺少配置自行添加</strong></li>
<li>make</li>
</ul>
<h3 id="2-Modern-C-mostly-safe-but-still-fast-and-low-level"><a href="#2-Modern-C-mostly-safe-but-still-fast-and-low-level" class="headerlink" title="2. Modern C++: mostly safe but still fast and low-level"></a>2. Modern C++: mostly safe but still fast and low-level</h3><p>意思是用现代C++</p>
<h3 id="3-Reading-the-Sponge-documentation"><a href="#3-Reading-the-Sponge-documentation" class="headerlink" title="3. Reading the Sponge documentation"></a>3. Reading the Sponge documentation</h3><p>就是给出来一些参考文档</p>
<h3 id="4-Writing-webget"><a href="#4-Writing-webget" class="headerlink" title="4. Writing webget"></a>4. Writing webget</h3><p>就是要自己实现一个get_URL函数<br>lab0给出了几个示例,首先观察socket-example2<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">uint16_t</span> portnum = ((std::<span class="built_in">random_device</span>()()) % <span class="number">50000</span>) + <span class="number">1025</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// create a TCP socket, bind it to a local address, and listen</span></span><br><span class="line">TCPSocket sock1;</span><br><span class="line">sock1.<span class="built_in">bind</span>(<span class="built_in">Address</span>(<span class="string">&quot;127.0.0.1&quot;</span>, portnum));</span><br><span class="line">sock1.<span class="built_in">listen</span>(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// create another socket and connect to the first one</span></span><br><span class="line">TCPSocket sock2;</span><br><span class="line">sock2.<span class="built_in">connect</span>(<span class="built_in">Address</span>(<span class="string">&quot;127.0.0.1&quot;</span>, portnum));</span><br><span class="line"></span><br><span class="line"><span class="comment">// accept the connection</span></span><br><span class="line"><span class="keyword">auto</span> sock3 = sock1.<span class="built_in">accept</span>();</span><br><span class="line">sock3.<span class="built_in">write</span>(<span class="string">&quot;hi there&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">auto</span> recvd = sock2.<span class="built_in">read</span>();</span><br><span class="line">sock2.<span class="built_in">write</span>(<span class="string">&quot;hi yourself&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">auto</span> recvd2 = sock3.<span class="built_in">read</span>();</span><br><span class="line"></span><br><span class="line">sock1.<span class="built_in">close</span>();              <span class="comment">// don&#x27;t need to accept any more connections</span></span><br><span class="line">sock2.<span class="built_in">close</span>();              <span class="comment">// you can call close(2) on a socket</span></span><br><span class="line">sock3.<span class="built_in">shutdown</span>(SHUT_RDWR);  <span class="comment">// you can also shutdown(2) a socket</span></span><br><span class="line"><span class="keyword">if</span> (recvd != <span class="string">&quot;hi there&quot;</span> || recvd2 != <span class="string">&quot;hi yourself&quot;</span>) &#123;</span><br><span class="line">    <span class="keyword">throw</span> std::<span class="built_in">runtime_error</span>(<span class="string">&quot;wrong data received&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>接下来需要我们仿造模拟一个get_URL函数<br>函数签名如下:<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">get_URL</span><span class="params">(<span class="keyword">const</span> string &amp;host, <span class="keyword">const</span> string &amp;path)</span> </span></span><br></pre></td></tr></table></figure><br>直接模仿示例写出代码<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">get_URL</span><span class="params">(<span class="keyword">const</span> string &amp;host, <span class="keyword">const</span> string &amp;path)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// Your code here.</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// You will need to connect to the &quot;http&quot; service on</span></span><br><span class="line">    <span class="comment">// the computer whose name is in the &quot;host&quot; string,</span></span><br><span class="line">    <span class="comment">// then request the URL path given in the &quot;path&quot; string.</span></span><br><span class="line">    TCPSocket ts;</span><br><span class="line">    <span class="comment">//使用http协议连接</span></span><br><span class="line">    ts.<span class="built_in">connect</span>(<span class="built_in">Address</span>(host,<span class="string">&quot;http&quot;</span>));</span><br><span class="line">	<span class="comment">//发送get信息</span></span><br><span class="line">    ts.<span class="built_in">write</span>(<span class="string">&quot;GET &quot;</span>+path+<span class="string">&quot; HTTP/1.1\r\nHost: &quot;</span>+host+<span class="string">&quot; \r\n\r\n&quot;</span>);</span><br><span class="line">    <span class="comment">//关闭</span></span><br><span class="line">    ts.<span class="built_in">shutdown</span>(SHUT_WR);</span><br><span class="line">    <span class="comment">//读出内容</span></span><br><span class="line">    <span class="keyword">while</span> (!ts.<span class="built_in">eof</span>())&#123;</span><br><span class="line">        cout&lt;&lt;ts.<span class="built_in">read</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Then you&#x27;ll need to print out everything the server sends back,</span></span><br><span class="line">    <span class="comment">// (not just one call to read() -- everything) until you reach</span></span><br><span class="line">    <span class="comment">// the &quot;eof&quot; (end of file).</span></span><br><span class="line">    <span class="comment">//关闭链接</span></span><br><span class="line">    ts.<span class="built_in">close</span>();</span><br><span class="line"><span class="comment">//    cerr &lt;&lt; &quot;Function called: get_URL(&quot; &lt;&lt; host &lt;&lt; &quot;, &quot; &lt;&lt; path &lt;&lt; &quot;).\n&quot;;</span></span><br><span class="line"><span class="comment">//    cerr &lt;&lt; &quot;Warning: get_URL() has not been implemented yet.\n&quot;;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>接下来执行make命令,然后执行./apps/webget cs144.keithw.org /hello,观察结果.</p>
<p><img src="image-20211018085619537.png" alt="image-20211018085619537"></p>
<p><strong>实验成功</strong></p>
<h2 id="An-in-memory-reliable-byte-stream"><a href="#An-in-memory-reliable-byte-stream" class="headerlink" title="An in-memory reliable byte stream"></a>An in-memory reliable byte stream</h2><p>简单来说，就是要实现一个用来支持给输入端写入数据和输出端读取数据的字节流缓冲区。<br>还需要设置的变量:</p>
<ol>
<li>缓冲区容量</li>
<li>输入端写完毕标志</li>
<li>写入和读取的字节数</li>
</ol>
<p>需要我们实现的函数如下:<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">ByteStream</span>(<span class="keyword">const</span> <span class="keyword">size_t</span> capacity);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \name &quot;Input&quot; interface for the writer</span></span><br><span class="line">    <span class="comment">//!@&#123;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//! Write a string of bytes into the stream. Write as many</span></span><br><span class="line">    <span class="comment">//! as will fit, and return how many were written.</span></span><br><span class="line">    <span class="comment">//! \returns the number of bytes accepted into the stream</span></span><br><span class="line">    <span class="function"><span class="keyword">size_t</span> <span class="title">write</span><span class="params">(<span class="keyword">const</span> std::string &amp;data)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \returns the number of additional bytes that the stream has space for</span></span><br><span class="line">    <span class="function"><span class="keyword">size_t</span> <span class="title">remaining_capacity</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! Signal that the byte stream has reached its ending</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">end_input</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! Indicate that the stream suffered an error.</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">set_error</span><span class="params">()</span> </span>&#123; _error = <span class="literal">true</span>; &#125;</span><br><span class="line">    <span class="comment">//!@&#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \name &quot;Output&quot; interface for the reader</span></span><br><span class="line">    <span class="comment">//!@&#123;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//! Peek at next &quot;len&quot; bytes of the stream</span></span><br><span class="line">    <span class="comment">//! \returns a string</span></span><br><span class="line">    <span class="function">std::string <span class="title">peek_output</span><span class="params">(<span class="keyword">const</span> <span class="keyword">size_t</span> len)</span> <span class="keyword">const</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! Remove bytes from the buffer</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">pop_output</span><span class="params">(<span class="keyword">const</span> <span class="keyword">size_t</span> len)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! Read (i.e., copy and then pop) the next &quot;len&quot; bytes of the stream</span></span><br><span class="line">    <span class="comment">//! \returns a string</span></span><br><span class="line">    <span class="function">std::string <span class="title">read</span><span class="params">(<span class="keyword">const</span> <span class="keyword">size_t</span> len)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \returns `true` if the stream input has ended</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">input_ended</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \returns `true` if the stream has suffered an error</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">error</span><span class="params">()</span> <span class="keyword">const</span> </span>&#123; <span class="keyword">return</span> _error; &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \returns the maximum amount that can currently be read from the stream</span></span><br><span class="line">    <span class="function"><span class="keyword">size_t</span> <span class="title">buffer_size</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \returns `true` if the buffer is empty</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">buffer_empty</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \returns `true` if the output has reached the ending</span></span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">eof</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line">    <span class="comment">//!@&#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//! \name General accounting</span></span><br><span class="line">    <span class="comment">//!@&#123;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//! Total number of bytes written</span></span><br><span class="line">    <span class="function"><span class="keyword">size_t</span> <span class="title">bytes_written</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//! Total number of bytes popped</span></span><br><span class="line">    <span class="function"><span class="keyword">size_t</span> <span class="title">bytes_read</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br></pre></td></tr></table></figure><br>缓冲区是fifo机制,这里选择使用队列,deque实现,因此需要额外添加的变量为<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//容量</span></span><br><span class="line">   <span class="keyword">size_t</span> capacity=<span class="number">0</span>;</span><br><span class="line">   <span class="comment">//缓冲区</span></span><br><span class="line">   std::deque&lt;<span class="keyword">char</span>&gt; buffer&#123;&#125;;</span><br><span class="line">   <span class="comment">//输入结束标志</span></span><br><span class="line">   <span class="keyword">bool</span> endInput=<span class="literal">false</span>;</span><br><span class="line">   <span class="comment">//写的字节数</span></span><br><span class="line">   <span class="keyword">size_t</span> bytesWritten=<span class="number">0</span>;</span><br><span class="line">   <span class="comment">//读的字节数</span></span><br><span class="line">   <span class="keyword">size_t</span> bytesRead=<span class="number">0</span>;</span><br></pre></td></tr></table></figure><br>下面给出各类函数的实现<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;byte_stream.hh&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Dummy implementation of a flow-controlled in-memory byte stream.</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// For Lab 0, please replace with a real implementation that passes the</span></span><br><span class="line"><span class="comment">// automated checks run by `make check_lab0`.</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// You will need to add private members to the class declaration in `byte_stream.hh`</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span>... Targs&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">DUMMY_CODE</span><span class="params">(Targs &amp;&amp;... <span class="comment">/* unused */</span>)</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line">ByteStream::<span class="built_in">ByteStream</span>(<span class="keyword">const</span> <span class="keyword">size_t</span> capacity1) :<span class="built_in">capacity</span>(capacity1)&#123; &#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">size_t</span> <span class="title">ByteStream::write</span><span class="params">(<span class="keyword">const</span> string &amp;data)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">remaining_capacity</span>()&lt;=<span class="number">0</span>)<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">size_t</span> canWrite=<span class="built_in">min</span>(<span class="built_in">remaining_capacity</span>(),data.<span class="built_in">size</span>());</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">size_t</span> i=<span class="number">0</span>;i&lt;canWrite;i++)&#123;</span><br><span class="line">        buffer.<span class="built_in">push_back</span>(data[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    bytesWritten+=canWrite;</span><br><span class="line">    <span class="keyword">return</span> canWrite;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//! \param[in] len bytes will be copied from the output side of the buffer 读取缓冲区前n个</span></span><br><span class="line"><span class="function">string <span class="title">ByteStream::peek_output</span><span class="params">(<span class="keyword">const</span> <span class="keyword">size_t</span> len)</span> <span class="keyword">const</span> </span>&#123;</span><br><span class="line">    <span class="keyword">size_t</span> canPeek=<span class="built_in">min</span>(len,<span class="built_in">buffer_size</span>());</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">string</span>().<span class="built_in">assign</span>(buffer.<span class="built_in">begin</span>(),buffer.<span class="built_in">begin</span>()+canPeek);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//! \param[in] len bytes will be removed from the output side of the buffer</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">ByteStream::pop_output</span><span class="params">(<span class="keyword">const</span> <span class="keyword">size_t</span> len)</span> </span>&#123;</span><br><span class="line"><span class="comment">//    DUMMY_CODE(len);</span></span><br><span class="line">    <span class="keyword">size_t</span> canPop=<span class="built_in">min</span>(len,<span class="built_in">buffer_size</span>());</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">size_t</span> i=<span class="number">0</span>;i&lt;canPop;i++)&#123;</span><br><span class="line">        buffer.<span class="built_in">pop_front</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    bytesRead+=canPop;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//! Read (i.e., copy and then pop) the next &quot;len&quot; bytes of the stream</span></span><br><span class="line"><span class="comment">//! \param[in] len bytes will be popped and returned</span></span><br><span class="line"><span class="comment">//! \returns a string</span></span><br><span class="line"><span class="comment">//! 返回读取的内容</span></span><br><span class="line"><span class="function">std::string <span class="title">ByteStream::read</span><span class="params">(<span class="keyword">const</span> <span class="keyword">size_t</span> len)</span> </span>&#123;</span><br><span class="line">    string resOfRead= <span class="built_in">peek_output</span>(len);</span><br><span class="line">    <span class="built_in">pop_output</span>(len);</span><br><span class="line">    <span class="keyword">return</span> resOfRead;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">ByteStream::end_input</span><span class="params">()</span> </span>&#123;endInput=<span class="literal">true</span>;&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">ByteStream::input_ended</span><span class="params">()</span> <span class="keyword">const</span> </span>&#123; <span class="keyword">return</span> endInput; &#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">size_t</span> <span class="title">ByteStream::buffer_size</span><span class="params">()</span> <span class="keyword">const</span> </span>&#123; <span class="keyword">return</span> buffer.<span class="built_in">size</span>(); &#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">ByteStream::buffer_empty</span><span class="params">()</span> <span class="keyword">const</span> </span>&#123; <span class="keyword">return</span> buffer.<span class="built_in">size</span>()==<span class="number">0</span>; &#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">ByteStream::eof</span><span class="params">()</span> <span class="keyword">const</span> </span>&#123; <span class="keyword">return</span> <span class="built_in">buffer_empty</span>()&amp;&amp;endInput; &#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">size_t</span> <span class="title">ByteStream::bytes_written</span><span class="params">()</span> <span class="keyword">const</span> </span>&#123; <span class="keyword">return</span> bytesWritten; &#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">size_t</span> <span class="title">ByteStream::bytes_read</span><span class="params">()</span> <span class="keyword">const</span> </span>&#123; <span class="keyword">return</span> bytesRead; &#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">size_t</span> <span class="title">ByteStream::remaining_capacity</span><span class="params">()</span> <span class="keyword">const</span> </span>&#123; <span class="keyword">return</span> capacity-buffer.<span class="built_in">size</span>(); &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></p>
<ul>
<li>执行结果如下</li>
</ul>
<p><img src="image-20211018094018241.png" alt="image-20211018094018241"></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/greenwood.gitee.io/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/" rel="tag"># 计算机网络</a>
              <a href="/greenwood.gitee.io/tags/CS144/" rel="tag"># CS144</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/greenwood.gitee.io/2021/10/17/Security/" rel="prev" title="Security">
      <i class="fa fa-chevron-left"></i> Security
    </a></div>
      <div class="post-nav-item">
    <a href="/greenwood.gitee.io/2021/10/18/2035/" rel="next" title="Leetcode2035 将数组分成两个数组并最小化数组和的差">
      Leetcode2035 将数组分成两个数组并最小化数组和的差 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#CS144-LAB0"><span class="nav-number">1.</span> <span class="nav-text">CS144 LAB0</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Fetch-a-Web-page"><span class="nav-number">1.1.</span> <span class="nav-text">Fetch a Web page</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#step1"><span class="nav-number">1.1.1.</span> <span class="nav-text">step1</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#step2"><span class="nav-number">1.1.2.</span> <span class="nav-text">step2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#step3-%E6%8B%93%E5%B1%95%E5%AE%9E%E9%AA%8C"><span class="nav-number">1.1.3.</span> <span class="nav-text">step3 拓展实验</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Listening-and-connecting"><span class="nav-number">1.2.</span> <span class="nav-text">Listening and connecting</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#step1-1"><span class="nav-number">1.2.1.</span> <span class="nav-text">step1</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#step2-1"><span class="nav-number">1.2.2.</span> <span class="nav-text">step2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#step3"><span class="nav-number">1.2.3.</span> <span class="nav-text">step3</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Writing-a-network-program-using-an-OS-stream-socket"><span class="nav-number">1.3.</span> <span class="nav-text">Writing a network program using an OS stream socket</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-fetching-and-building-the-starter-code"><span class="nav-number">1.3.1.</span> <span class="nav-text">1. fetching and building the starter code</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-Modern-C-mostly-safe-but-still-fast-and-low-level"><span class="nav-number">1.3.2.</span> <span class="nav-text">2. Modern C++: mostly safe but still fast and low-level</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-Reading-the-Sponge-documentation"><span class="nav-number">1.3.3.</span> <span class="nav-text">3. Reading the Sponge documentation</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-Writing-webget"><span class="nav-number">1.3.4.</span> <span class="nav-text">4. Writing webget</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#An-in-memory-reliable-byte-stream"><span class="nav-number">1.4.</span> <span class="nav-text">An in-memory reliable byte stream</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Dybala</p>
  <div class="site-description" itemprop="description">日子越来越有判头了</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/greenwood.gitee.io/archives/">
        
          <span class="site-state-item-count">22</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/greenwood.gitee.io/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/greenwood.gitee.io/tags/">
          
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Dybala</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/greenwood.gitee.io/lib/anime.min.js"></script>
  <script src="/greenwood.gitee.io/lib/velocity/velocity.min.js"></script>
  <script src="/greenwood.gitee.io/lib/velocity/velocity.ui.min.js"></script>

<script src="/greenwood.gitee.io/js/utils.js"></script>

<script src="/greenwood.gitee.io/js/motion.js"></script>


<script src="/greenwood.gitee.io/js/schemes/pisces.js"></script>


<script src="/greenwood.gitee.io/js/next-boot.js"></script>




  















  

  

  

</body>
</html>
